package com.gitee.pifeng.monitoring.ui.business.web.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.pifeng.monitoring.common.dto.BaseRequestPackage;
import com.gitee.pifeng.monitoring.plug.core.Sender;
import com.gitee.pifeng.monitoring.ui.business.web.dao.IMonitorFeedDao;
import com.gitee.pifeng.monitoring.ui.business.web.dao.IMonitorReportDao;
import com.gitee.pifeng.monitoring.ui.business.web.entity.MonitorFeed;
import com.gitee.pifeng.monitoring.ui.business.web.entity.MonitorReport;
import com.gitee.pifeng.monitoring.ui.business.web.entity.MonitorUser;
import com.gitee.pifeng.monitoring.ui.business.web.service.IMonitorFeedService;
import com.gitee.pifeng.monitoring.ui.business.web.vo.LayUiAdminResultVo;
import com.gitee.pifeng.monitoring.ui.business.web.vo.MonitorFeedVo;
import com.gitee.pifeng.monitoring.ui.constant.UrlConstants;
import com.gitee.pifeng.monitoring.ui.constant.WebResponseConstants;
import com.gitee.pifeng.monitoring.ui.core.PackageConstructor;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.hyperic.sigar.SigarException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class IMonitorFeedServiceImpl implements IMonitorFeedService {

    @Autowired
    private IMonitorFeedDao monitorFeedDao;
    @Autowired
    private IMonitorReportDao monitorReportDao;
    @Override
    public Page<MonitorFeedVo> getMonitorFeedList(Long current, Long size,String feedNumber, String submitUser, String solveUser, Integer status) {
        IPage<MonitorFeed> ipage = new Page<>(current, size);
        LambdaQueryWrapper<MonitorFeed> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(MonitorFeed ::getId,MonitorFeed::getFeedNumber,MonitorFeed::getFeedTitle,
                MonitorFeed::getSubmitUser,MonitorFeed::getSubmitTime,MonitorFeed::getSolveUser,MonitorFeed::getSolveTime,
                MonitorFeed::getStatus,MonitorFeed::getAppraise);
        if (StringUtils.isNotBlank(feedNumber)){
            lambdaQueryWrapper.like(MonitorFeed::getFeedNumber,feedNumber);
        }
        if (StringUtils.isNotBlank(submitUser)){
            lambdaQueryWrapper.like(MonitorFeed::getSubmitUser,submitUser);
        }
        if (StringUtils.isNotBlank(solveUser)){
            lambdaQueryWrapper.like(MonitorFeed::getSolveUser,solveUser);
        }
        if (status!=null){
            lambdaQueryWrapper.like(MonitorFeed::getStatus,status);
        }
        IPage<MonitorFeed> monitorFeedPage = this.monitorFeedDao.selectPage(ipage, lambdaQueryWrapper);
        List<MonitorFeed> records = monitorFeedPage.getRecords();
        List<MonitorFeedVo> monitorFeedVos = Lists.newLinkedList();
        for (MonitorFeed record : records) {
            MonitorFeedVo monitorFeedVo = MonitorFeedVo.builder().build().convertFor(record);
            monitorFeedVos.add(monitorFeedVo);
        }
        Page<MonitorFeedVo> objectPage = new Page<>();
        objectPage.setRecords(monitorFeedVos);
        objectPage.setTotal(monitorFeedPage.getTotal());
        return objectPage;
    }

    @Override
    public LayUiAdminResultVo getLast7DayFeed() {
        List<Map<Object, String>> last7DayFeed = monitorFeedDao.getLast7DayFeed();
        return LayUiAdminResultVo.ok(last7DayFeed);
    }

    @Override
    public LayUiAdminResultVo getUserFeed() {
        List<Map<Object, String>> userFeed= monitorFeedDao.getUserFeed();
        return LayUiAdminResultVo.ok(userFeed);
    }

    @Override
    public void sendFeedMail(String toMail,String type) throws SigarException, IOException {
        // 封装请求数据
        JSONObject extraMsg = new JSONObject();
        extraMsg.put("toMail", toMail);
        extraMsg.put("type",type);
        BaseRequestPackage baseRequestPackage = new PackageConstructor().structureBaseRequestPackage(extraMsg);
        String resultStr = Sender.send(UrlConstants.FEEDBACK_SEND_EMAIL, baseRequestPackage.toJsonString());
    }

    @Override
    public LayUiAdminResultVo getFeedDepartment() {
        List<Map<Object, String>> feedDepartment = monitorFeedDao.getFeedDepartment();
        return LayUiAdminResultVo.ok(feedDepartment);
    }

    @Override
    public Page<MonitorFeedVo> getCommonMonitorFeedList(Long current, Long size, String feedNumber, Integer status, String username, String solveUser) {
        IPage<MonitorFeed> ipage = new Page<>(current, size);
        LambdaQueryWrapper<MonitorFeed> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(MonitorFeed ::getId,MonitorFeed::getFeedNumber,MonitorFeed::getFeedTitle,MonitorFeed::getSubmitTime,MonitorFeed::getSolveUser,MonitorFeed::getSolveTime,
                MonitorFeed::getStatus,MonitorFeed::getAppraise);
        if (StringUtils.isNotBlank(feedNumber)){
            lambdaQueryWrapper.like(MonitorFeed::getFeedNumber,feedNumber);
        }
        if (StringUtils.isNotBlank(solveUser)){
            lambdaQueryWrapper.like(MonitorFeed::getSolveUser,solveUser);
        }
        if (status!=null){
            lambdaQueryWrapper.like(MonitorFeed::getStatus,status);
        }
        lambdaQueryWrapper.like(MonitorFeed::getSubmitUser,username);
        IPage<MonitorFeed> monitorFeedPage = this.monitorFeedDao.selectPage(ipage, lambdaQueryWrapper);
        List<MonitorFeed> records = monitorFeedPage.getRecords();
        List<MonitorFeedVo> monitorFeedVos = Lists.newLinkedList();
        for (MonitorFeed record : records) {
            MonitorFeedVo monitorFeedVo = MonitorFeedVo.builder().build().convertFor(record);
            monitorFeedVos.add(monitorFeedVo);
        }
        Page<MonitorFeedVo> objectPage = new Page<>();
        objectPage.setRecords(monitorFeedVos);
        objectPage.setTotal(monitorFeedPage.getTotal());
        return objectPage;
    }

    @Override
    public Page<MonitorFeedVo> getRepairMonitorFeedList(Long current, Long size, String feedNumber, Integer status,String userName,String submitUser) {
        IPage<MonitorFeed> ipage = new Page<>(current, size);
        LambdaQueryWrapper<MonitorFeed> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(MonitorFeed ::getId,MonitorFeed::getFeedNumber,MonitorFeed::getFeedTitle,
                MonitorFeed::getSubmitUser,MonitorFeed::getSubmitTime,MonitorFeed::getSolveUser,MonitorFeed::getSolveTime,
                MonitorFeed::getStatus,MonitorFeed::getAppraise,MonitorFeed::getCantReport);
        if (StringUtils.isNotBlank(feedNumber)){
            lambdaQueryWrapper.like(MonitorFeed::getFeedNumber,feedNumber);
        }
        if (status!=null){
            lambdaQueryWrapper.like(MonitorFeed::getStatus,status);
        }
        if (StringUtils.isNotBlank(submitUser)){
            lambdaQueryWrapper.like(MonitorFeed::getSubmitUser,submitUser);
        }
        lambdaQueryWrapper.like(MonitorFeed::getSolveUser,userName);
        IPage<MonitorFeed> monitorFeedPage = this.monitorFeedDao.selectPage(ipage, lambdaQueryWrapper);
        List<MonitorFeed> records = monitorFeedPage.getRecords();
        List<MonitorFeedVo> monitorFeedVos = Lists.newLinkedList();
        for (MonitorFeed record : records) {
            MonitorFeedVo monitorFeedVo = MonitorFeedVo.builder().build().convertFor(record);
            monitorFeedVos.add(monitorFeedVo);
        }
        Page<MonitorFeedVo> objectPage = new Page<>();
        objectPage.setRecords(monitorFeedVos);
        objectPage.setTotal(monitorFeedPage.getTotal());
        return objectPage;
    }

    @Override
    public LayUiAdminResultVo addReport(String id, String content,Integer a) {
        MonitorReport monitorReport = new MonitorReport();
        monitorReport.setFeedNumber(id);
        MonitorFeed monitorFeed = monitorFeedDao.selectById(id);
        monitorFeed.setCantReport(1);
        monitorFeedDao.updateById(monitorFeed);
        monitorReport.setContent(content);
        int insert = monitorReportDao.insert(monitorReport);
        if (insert>0){
            return LayUiAdminResultVo.ok(WebResponseConstants.SUCCESS);
        }
        return LayUiAdminResultVo.ok(WebResponseConstants.FAIL);
    }

    @Override
    public LayUiAdminResultVo saveMonitorFeed(MonitorFeed monitorFeed) {
        int insert = monitorFeedDao.insert(monitorFeed);
        if (insert>0){
            return LayUiAdminResultVo.ok(WebResponseConstants.SUCCESS);
        }
        return LayUiAdminResultVo.ok(WebResponseConstants.FAIL);
    }


    @Override
    public MonitorFeedVo getFeedDetail(String id) {
        MonitorFeed monitorFeed = monitorFeedDao.selectById(id);
        MonitorFeedVo monitorFeedVo = MonitorFeedVo.builder().build().convertFor(monitorFeed);
        return monitorFeedVo;
    }

    @Override
    public LayUiAdminResultVo deleteFeed(List<MonitorFeedVo> monitorFeedVos) {
        List<Long> ids = Lists.newArrayList();
        for (MonitorFeedVo monitorFeedVo : monitorFeedVos) {
            ids.add(monitorFeedVo.getId());
        }
        LambdaUpdateWrapper<MonitorFeed> monitorFeedLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        monitorFeedLambdaUpdateWrapper.in(MonitorFeed::getId,ids);
        this.monitorFeedDao.delete(monitorFeedLambdaUpdateWrapper);
        return LayUiAdminResultVo.ok(WebResponseConstants.SUCCESS);
    }

    @Override
    public MonitorFeed selectByid(String id) {
        return this.monitorFeedDao.selectById(id);
    }

    @Override
    public LayUiAdminResultVo updateMonitorFeed(MonitorFeed monitorFeed) {
        this.monitorFeedDao.updateById(monitorFeed);
        return LayUiAdminResultVo.ok(WebResponseConstants.SUCCESS);
    }
}
